#ifndef __UDSF_DEMO_RADAR_APP_LP_TRIGGER_C__
#define __UDSF_DEMO_RADAR_APP_LP_TRIGGER_C__

#include "app.h"


#if DEMO_CFG == DEMO_CFG_RADAR_APP_LP_TRIGGER && defined(APP_DEMO_ENABLE)
/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "udsf_xxx.c"形式引用
*/

#include "../udsf/udsf_app_init.c"
#include "../udsf/udsf_uart.c"
#include "../udsf/udsf_spi.c"
#include "../udsf/udsf_spiflash.c"
#include "../udsf/udsf_gpio.c"
#include "../udsf/udsf_rtc.c"
#include "../udsf/udsf_delay.c"
#include "../udsf/udsf_protocol_debug.c"
#include "../udsf/udsf_protocol_pcrst.c"
#include "../udsf/udsf_lpw.c"
#include "../udsf/udsf_app_switch.c"
#include "../udsf/udsf_cstdlike.c"
#include "../udsf/udsf_radar_dacq.c"
#include "../udsf/udsf_radar_bbe.c"
#include "../udsf/udsf_radar_cfar.c"
#include "../udsf/udsf_bbe.c"
#include "../udsf/udsf_udk_addr_get.c"
#include "../udsf/udsf_timera.c"
#include "../udsf/udsf_data_format_convert.c"
#include "../udsf/udsf_ramp.c"
#include "../udsf/udsf_i2c_slave.c"
#if (DEBUG_DATA_INTERFACE == 1) || (LOG_INTERFACE == 1) || ( CUSTOM_DATA_INTERFACE == 1 ) || (DEBUG_DATA_INTERFACE == 1)
    #include "udsf_ft232.c"
#endif

//#if defined(UART_PARAM_CFG_ENABLE)
//#if ENABLE_UDF11 < 1
//    #error xxxxx
//#endif

//#include "udsf_app_cfg.c"

///*本函数 基于字符串 演示参数A/B区配置*/
//static int udsf_demo_radar_app_lp_trigger_param_cfg(void)
//{
//    //CSTD_PRINTF("UDSF11 \n");


//    if (paramRT->switch_flag == 0)  // 仅首次启动，独立APP切换回来不执行
//    {
//        //          udsf_app_init(NULL);

//        UDK_ASSERT((*p_boot_flag == APPLICATION_MODE));

//        #if SDK_VERSION < SDK_VERSION_GENE(3,0)
//        UDK_ASSERT((APPLICATION_MODE == ROM_STARTUP));
//        #endif

//        #if (IS_RAMAPP>0)
//        #if SDK_VERSION >= SDK_VERSION_GENE(3,0)
//        #else
//        udsf_uart_init(paramFunc->baudrate, APPLICATION_MODE);
//        GPIO->PMODE  = paramFunc->gpio_pmode;
//        PIN_CTRL->UE = paramFunc->gpio_pctrl_ue;
//        PIN_CTRL->RE = paramFunc->gpio_pctrl_re;
//        PINMUX->MUX0 = paramFunc->gpio_pmux0;
//        PINMUX->MUX1 = paramFunc->gpio_pmux1;
//        #endif
//        //        udsf_delay_ms(20);
//        //        udsf_uart_send_string("AT+OK\n");
//        #endif

//        /**
//        * user add: startup hardware init; start
//        */


//        #if TEST_MODE == 0
//        #if (LOG_INTERFACE == 1)
//        udsf_ft232_init();
//        #endif

//        #endif

//        #if defined( GPIO_OUTPUT_ENABLE ) && defined(APP_INDEPENDENT)
//        udsf_pinctrl_od(1 << GPIO_OUTPUT_ID, GPIO_OUTPUT_MODE);          // od or pp
//        udsf_pinctrl_re(1 << GPIO_OUTPUT_ID, GPIO_OUTPUT_PULL);   // 上拉
//        udsf_pinctrl_ue(1 << GPIO_OUTPUT_ID, GPIO_OUTPUT_PULL);   // 上拉
//        udsf_gpio_output(GPIO_OUTPUT_ID, GPIO_OUTPUT_ACTIVE);   // 输出
//        udsf_gpio_output_ctrl(1 << GPIO_OUTPUT_ID, 1);          // 输出
//        GPIO_OUTPUT_SET = GPIO_OUTPUT_ACTIVE;                   // 输出高
//        udsf_delay_ms(2000);   // 开机短亮 以肉眼识别复位
//        #endif


//        /**
//        * user add: startup hardware init; end
//        */

//        if (paramFunc->baudrate > 0)
//        {
//            udsf_uart_send_string("version:");
//            udsf_uart_send_string(FW_VERSION);
//            udsf_uart_send_string("\n");
//        }
//    }

//    if (sUDF_global.param_cfg_status == 0)
//    {
//        // 查找AB区，加载配置到paramCFG
//        #if SDK_VERSION < SDK_VERSION_GENE(3,0)
//        udsf_cfg_init(*p_boot_flag);
//        #endif

//        if (paramRT->ab_cfg_flag > CFG_PLACE_USR_NONE)
//        {
//            udsf_cfg_load(*p_boot_flag, paramRT->ab_cfg_flag - 1, (uint8_t *)paramCFG, sizeof(PARAM_CFG));
//        }

//        int xx = paramRT->ab_cfg_flag - 1;
//        // NOTE: romv2仅有限支持printf，谨慎使用。
//        CSTD_PRINTF("cfg a/b %d\n", xx);

//        sUDF_global.param_cfg_status = 1;
//    }
//    else
//    {
//        #ifdef UART_RECV_ENABLE
//        udsf_uart_recv_init();
//        #endif

//        /* PARAM_CFG 简易配置示例 */
//#define PARA_TYPE_U8  (0)
//#define PARA_TYPE_U16 (1)
//#define PARA_TYPE_U32 (2)

//        struct cfg_table
//        {
//            void *p_para;
//            uint8_t para_type;
//            uint8_t para_len; // 暂只支持1
//        };
//        struct cfg_table cfg_table[] =
//        {
//            {(void *)&paramCFG->trigger_range_threshold, PARA_TYPE_U16, 1},
//            {(void *)&paramCFG->no_target_cnt_timeout, PARA_TYPE_U16, 1},
//        };

//        uint8_t recv_cmd_data[50 + 1];

//        while (1)
//        {

//            WDT->STR = 1;

//            int len;

//            if ((len = udsf_uart_recv_frame_data(recv_cmd_data, sizeof(recv_cmd_data) - 1)) >= 0)
//            {

//                recv_cmd_data[len] = '\0';

//                if (udsf_strstr((const char *)recv_cmd_data, "config exit and save;"))      // 退出并固化
//                {

//                    int res = -1;

//                    #if SDK_VERSION >= SDK_VERSION_GENE(3,0)
//                    uint8_t *src_addr = (uint8_t *)paramCFG;
//                    uint32_t len      = sizeof(PARAM_CFG);
//                    #else
//                    uint8_t *src_addr = (uint8_t *)paramRT;
//                    uint32_t len      = sizeof(PARAM_CFG) + sizeof(PARAM_RUNTIME);
//                    #endif

//                    udsf_cfg_update_crc((uint8_t *)src_addr, len);

//                    if (paramRT->ab_cfg_flag > CFG_PLACE_USR_NONE)
//                    {
//                        res = udsf_cfg_save(*p_boot_flag, paramRT->ab_cfg_flag - 1, src_addr, len, 0);

//                        if (res >= 0)
//                        {
//                            paramRT->ab_cfg_flag = res + 1;
//                        }
//                    }

//                    else   // 无A/B区配置
//                    {
//                        res = udsf_cfg_save(*p_boot_flag, CFG_PLACE_USR_B - 1, src_addr, len, 1);   // 强制写A区

//                        if (res >= 0)
//                        {
//                            paramRT->ab_cfg_flag = res + 1;
//                        }
//                    }

//                    //CSTD_PRINTF(("current ab %d\n",paramRT->ab_cfg_flag-1);

//                    if (res < 0)
//                    {
//                        udsf_uart_send_string("config exit and save failed!\n");
//                    }

//                    else
//                    {
//                        udsf_uart_send_string("config exit and save succ!\n");
//                        // NOTE: 若APP 未能适配paramCFG动态变更，此处应调用WDT_RSTNOW整机复位
//                        break;
//                    }

//                }

//                else if (udsf_strstr((const char *)recv_cmd_data, "config exit;"))      // 退出不固化
//                {
//                    #if SDK_VERSION >= SDK_VERSION_GENE(3,0)
//                    udsf_cfg_update_crc((uint8_t *)paramCFG, sizeof(PARAM_CFG));
//                    #else
//                    udsf_cfg_update_crc((uint8_t *)paramRT, sizeof(PARAM_CFG) + sizeof(PARAM_RUNTIME));
//                    #endif
//                    udsf_uart_send_string("config exit!\n");
//                    break;
//                    // NOTE: 若APP 未能适配paramCFG动态变更，此处应调用WDT_RSTNOW整机复位
//                }

//                else if (udsf_strstr((const char *)recv_cmd_data, "config give up;"))      // 放弃修改
//                {
//                    int res = -1;

//                    if (paramRT->ab_cfg_flag > CFG_PLACE_USR_NONE)   // 有配置区
//                    {
//                        res = udsf_cfg_load(*p_boot_flag, paramRT->ab_cfg_flag - 1, (uint8_t *)paramCFG, sizeof(PARAM_CFG));
//                    }

//                    if (res == 0)
//                    {
//                        udsf_uart_send_string("config give up succ!\n");
//                    }

//                    else
//                    {
//                        udsf_uart_send_string("config give up fail!\n");
//                    }
//                }

//                else if (udsf_strstr((const char *)recv_cmd_data, "config recovery;"))      // 恢复默认
//                {
//                    udsf_cfg_default(*p_boot_flag, *p_app_location, *p_app_offset, (uint8_t *)paramCFG, sizeof(PARAM_CFG));
//                    udsf_uart_send_string("config recovery!\n");
//                }

//                else
//                {
//                    int valid = -1;

//                    if (udsf_memcmp(recv_cmd_data, "set=", udsf_strlen("set=")) == 0)
//                    {
//                        valid = 0;
//                        //udsf_uart_send_string("aaaa\n");
//                    }

//                    else if (udsf_memcmp(recv_cmd_data, "get=", udsf_strlen("get=")) == 0)
//                    {
//                        //udsf_uart_send_string("bbbb\n");
//                        valid = 1;
//                    }

//                    int status = -1;
//#define SET_GET_CMD_LEN (4)

//                    if (valid >= 0)
//                    {
//                        uint8_t paraID[50 + 1];
//                        udsf_memset(paraID, 0, sizeof(paraID));
//                        uint8_t paraData[50 + 1];
//                        udsf_memset(paraData, 0, sizeof(paraData));

//                        do
//                        {
//                            char *p = udsf_strstr((const char *)recv_cmd_data, ",");

//                            if (p == NULL)
//                            {
//                                break;
//                            }

//                            udsf_memcpy(&recv_cmd_data[SET_GET_CMD_LEN], paraID, (uint32_t)p - (uint32_t)recv_cmd_data - SET_GET_CMD_LEN);

//                            if ((uint32_t)p + 1 < (uint32_t)recv_cmd_data + udsf_strlen((char *)recv_cmd_data) - 1)
//                            {
//                                udsf_memcpy((uint8_t *)p + 1, paraData, (uint32_t)recv_cmd_data + udsf_strlen((char *)recv_cmd_data) - (uint32_t)p - 2);
//                            }

//                            //                              udsf_uart_send_string(paraID);
//                            //                              udsf_uart_send_string("\n");
//                            //                              udsf_uart_send_string(paraData);
//                            //                              udsf_uart_send_string("\n");

//                            int para_id_len = udsf_strlen((char *)paraID);

//                            for (int i = 0; i < para_id_len; i++)
//                            {
//                                if ((paraID[i] < '0') || (paraID[i] > '9'))
//                                {
//                                    para_id_len = -1;
//                                    break;
//                                }
//                            }

//                            if (para_id_len <= 0)
//                            {
//                                break; // 无 para_id
//                            }

//                            uint32_t para_id_value = 0;

//                            for (int i = 0; i < para_id_len; i++)
//                            {
//                                para_id_value *= 10;
//                                para_id_value += paraID[i] - '0';
//                            }

//                            if (para_id_value >= sizeof(cfg_table) / sizeof(cfg_table[0]))
//                            {
//                                break; // para_id 超范围
//                            }

//                            int para_data_len = udsf_strlen((char *)paraData);

//                            for (int i = 0; i < para_data_len; i++)
//                            {
//                                if ((paraData[i] < '0') || (paraData[i] > '9'))
//                                {
//                                    para_data_len = -1;
//                                    break;
//                                }
//                            }

//                            uint32_t para_data_value = 0;

//                            if (para_data_len > 0)
//                            {
//                                for (int i = 0; i < para_data_len; i++)
//                                {
//                                    para_data_value *= 10;
//                                    para_data_value += paraData[i] - '0';
//                                }
//                            }

//                            if (valid)   // read
//                            {
//                                // NOTE: romv2仅有限支持printf，谨慎使用。
//                                if (cfg_table[para_id_value].para_type == PARA_TYPE_U8)
//                                {
//                                    CSTD_PRINTF("get=%d,%d!\n", para_id_value, *(uint8_t *)cfg_table[para_id_value].p_para);
//                                }

//                                else if (cfg_table[para_id_value].para_type == PARA_TYPE_U16)
//                                {
//                                    CSTD_PRINTF("get=%d,%d!\n", para_id_value, *(uint16_t *)cfg_table[para_id_value].p_para);
//                                }

//                                else if (cfg_table[para_id_value].para_type == PARA_TYPE_U32)
//                                {
//                                    CSTD_PRINTF("get=%d,%d!\n", para_id_value, *(uint32_t *)cfg_table[para_id_value].p_para);
//                                }
//                            }

//                            else   // set
//                            {
//                                if (para_data_len <= 0)
//                                {
//                                    break;
//                                }

//                                if (cfg_table[para_id_value].para_type == PARA_TYPE_U8)
//                                {
//                                    *(uint8_t *)cfg_table[para_id_value].p_para = para_data_value;
//                                    para_data_value = (uint8_t)para_data_value;
//                                }

//                                else if (cfg_table[para_id_value].para_type == PARA_TYPE_U16)
//                                {
//                                    *(uint16_t *)cfg_table[para_id_value].p_para = para_data_value;
//                                    para_data_value = (uint16_t)para_data_value;
//                                }

//                                else if (cfg_table[para_id_value].para_type == PARA_TYPE_U32)
//                                {
//                                    *(uint32_t *)cfg_table[para_id_value].p_para = para_data_value;
//                                    para_data_value = (uint32_t)para_data_value;
//                                }

//                                // NOTE: romv2仅有限支持printf，谨慎使用。
//                                CSTD_PRINTF("set=%d,%d!\n", para_id_value, para_data_value);
//                            }

//                            status = 0;
//                        }
//                        while (0);

//                        if (status < 0)
//                        {
//                            udsf_uart_send_string((char *)recv_cmd_data);
//                            udsf_uart_send_string(" err\n");
//                        }
//                    }

//                    else
//                    {
//                        udsf_uart_send_string("unknown cmd: ");
//                        udsf_uart_send_string((char *)recv_cmd_data);
//                        udsf_uart_send_string("!\n");
//                    }
//                }
//            }
//        }
//    }

//    sUDF_global.config_mode_exit = 1;

//    paramRT->switch_flag = 1; // return 前应该置位该标志位
//    #if SDK_VERSION < SDK_VERSION_GENE(3,0)
//    return UNIVERSAL_UDF_RES_GO_UDF03; // 返回UDF03
//    #else
//    return UNIVERSAL_UDF_RES_GO_UDF05; // 返回UDF05
//    #endif
//}
//#else
//static int udsf_demo_radar_app_lp_trigger_param_cfg(void)
//{
//    paramRT->switch_flag = 1; // return 前应该置位该标志位
//    #if SDK_VERSION < SDK_VERSION_GENE(3,0)
//    return UNIVERSAL_UDF_RES_GO_UDF03; // 返回UDF03
//    #else
//    return UNIVERSAL_UDF_RES_GO_UDF05; // 返回UDF05
//    #endif
//}
//#endif

static void udsf_demo_radar_app_lp_trigger_init(void)
{

    if (paramRT->startup_flag)
    {
//        paramRT->startup_flag = 0;
        paramRT->frame_cnt    = 0;

        /**
        * user add: startup software init; start
        */
        sUDF_global.has_target_cnt_lck = 0;
        sUDF_global.frame_fifo_index = 0;
        #if defined(APP_HAS_MULTI_UDF) && (ENABLE_UDF13>0) && (SDK_VERSION < SDK_VERSION_GENE(3,0))
        udsf_app_task_info_backup(&sUDF_global.task_info);
        #endif

        #if defined(UART_RECV_ENABLE)
        sUDF_global.uart_wait_cnt_ms = 0;
        #endif

        #if defined(UART_PARAM_CFG_ENABLE)
        sUDF_global.param_cfg_status = 0;
        sUDF_global.config_mode_exit = 0;
        #endif
        /**
        * user add: startup software init; end
        */

        if (paramRT->switch_flag == 0) // 仅首次启动，独立APP切换回来不执行
        {
            //udsf_app_init(NULL);
            //UDK_ASSERT((*p_boot_flag == APPLICATION_MODE));
        }

        PREPDATA_HANDLE();

        #if defined(FLASH_WINRANGE) && defined(WINRANGE_ENABLE)

        #if (IS_RAMAPP == 1)

        #if UDFDEF_ADCSAMP_NUM == 256
        const uint16_t Taylor_winRange_256[256]
            = {0x03dc, 0x03de, 0x03e4, 0x03eb, 0x03f5, 0x0402, 0x0412, 0x0423, 0x0438, 0x044e, 0x0468, 0x0483, 0x04a1, 0x04c2, 0x04e4, 0x0509, 0x0531, 0x055a, 0x0586, 0x05b3, 0x05e3, 0x0615, 0x0648, 0x067e, 0x06b5, 0x06ef, 0x072a, 0x0767, 0x07a5, 0x07e5, 0x0826, 0x0869, 0x08ae, 0x08f3, 0x093a, 0x0983, 0x09cc, 0x0a17, 0x0a62, 0x0aaf, 0x0afc, 0x0b4b, 0x0b9a, 0x0bea, 0x0c3b, 0x0c8c, 0x0cde, 0x0d31, 0x0d84, 0x0dd7, 0x0e2b, 0x0e80, 0x0ed4, 0x0f29, 0x0f7e, 0x0fd4, 0x1029, 0x107e, 0x10d4, 0x1129, 0x117f, 0x11d4, 0x1229, 0x127f, 0x12d3, 0x1328, 0x137c, 0x13d0, 0x1424, 0x1477, 0x14ca, 0x151d, 0x156e, 0x15c0, 0x1611, 0x1661, 0x16b0, 0x16ff, 0x174d, 0x179b, 0x17e7, 0x1833, 0x187e, 0x18c8, 0x1911, 0x1959, 0x19a0, 0x19e6, 0x1a2b, 0x1a6f, 0x1ab2, 0x1af4, 0x1b34, 0x1b73, 0x1bb1, 0x1bee, 0x1c29, 0x1c63, 0x1c9b, 0x1cd2, 0x1d07, 0x1d3b, 0x1d6d, 0x1d9e, 0x1dcd, 0x1dfa, 0x1e26, 0x1e50, 0x1e78, 0x1e9e, 0x1ec3, 0x1ee5, 0x1f06, 0x1f25, 0x1f42, 0x1f5d, 0x1f75, 0x1f8c, 0x1fa1, 0x1fb4, 0x1fc5, 0x1fd4, 0x1fe0, 0x1feb, 0x1ff3, 0x1ffa, 0x1ffe, 0x1fff, 0x1fff, 0x1ffe, 0x1ffa, 0x1ff3, 0x1feb, 0x1fe0, 0x1fd4, 0x1fc5, 0x1fb4, 0x1fa1, 0x1f8c, 0x1f75, 0x1f5d, 0x1f42, 0x1f25, 0x1f06, 0x1ee5, 0x1ec3, 0x1e9e, 0x1e78, 0x1e50, 0x1e26, 0x1dfa, 0x1dcd, 0x1d9e, 0x1d6d, 0x1d3b, 0x1d07, 0x1cd2, 0x1c9b, 0x1c63, 0x1c29, 0x1bee, 0x1bb1, 0x1b73, 0x1b34, 0x1af4, 0x1ab2, 0x1a6f, 0x1a2b, 0x19e6, 0x19a0, 0x1959, 0x1911, 0x18c8, 0x187e, 0x1833, 0x17e7, 0x179b, 0x174d, 0x16ff, 0x16b0, 0x1661, 0x1611, 0x15c0, 0x156e, 0x151d, 0x14ca, 0x1477, 0x1424, 0x13d0, 0x137c, 0x1328, 0x12d3, 0x127f, 0x1229, 0x11d4, 0x117f, 0x1129, 0x10d4, 0x107e, 0x1029, 0x0fd4, 0x0f7e, 0x0f29, 0x0ed4, 0x0e80, 0x0e2b, 0x0dd7, 0x0d84, 0x0d31, 0x0cde, 0x0c8c, 0x0c3b, 0x0bea, 0x0b9a, 0x0b4b, 0x0afc, 0x0aaf, 0x0a62, 0x0a17, 0x09cc, 0x0983, 0x093a, 0x08f3, 0x08ae, 0x0869, 0x0826, 0x07e5, 0x07a5, 0x0767, 0x072a, 0x06ef, 0x06b5, 0x067e, 0x0648, 0x0615, 0x05e3, 0x05b3, 0x0586, 0x055a, 0x0531, 0x0509, 0x04e4, 0x04c2, 0x04a1, 0x0483, 0x0468, 0x044e, 0x0438, 0x0423, 0x0412, 0x0402, 0x03f5, 0x03eb, 0x03e4, 0x03de, 0x03dc};
        #elif UDFDEF_ADCSAMP_NUM == 64
        const uint16_t Taylor_winRange_64[64]
            = {0x03e1, 0x040a, 0x045b, 0x04d3, 0x0570, 0x062f, 0x070d, 0x0806, 0x0918, 0x0a3e, 0x0b74, 0x0cb7, 0x0e03, 0x0f56, 0x10ab, 0x1201, 0x1355, 0x14a3, 0x15eb, 0x1729, 0x185c, 0x1980, 0x1a94, 0x1b96, 0x1c83, 0x1d58, 0x1e14, 0x1eb5, 0x1f37, 0x1f9b, 0x1fde, 0x1fff, 0x1fff, 0x1fde, 0x1f9b, 0x1f37, 0x1eb5, 0x1e14, 0x1d58, 0x1c83, 0x1b96, 0x1a94, 0x1980, 0x185c, 0x1729, 0x15eb, 0x14a3, 0x1355, 0x1201, 0x10ab, 0x0f56, 0x0e03, 0x0cb7, 0x0b74, 0x0a3e, 0x0918, 0x0806, 0x070d, 0x062f, 0x0570, 0x04d3, 0x045b, 0x040a, 0x03e1};
        #endif

        uint16_t *win_dst = (uint16_t *)UDFDEF_ADDR_WIN_RANGE;
        #if UDFDEF_ADCSAMP_NUM == 64
        const uint16_t *win_src = Taylor_winRange_64;
        #else
        const uint16_t *win_src = Taylor_winRange_256;
        #endif

        udsf_memcpy32((uint32_t *)win_src, (uint32_t *)win_dst, UDFDEF_ADCSAMP_NUM / 2);       // UDFDEF_ADCSAMP_NUM*2/4 DWORD

        #else
        uint32_t flash_addr = udsf_udk_addr_get_app_rsv(*p_app_location);
        #if UDFDEF_ADCSAMP_NUM == 64
        udsf_memory_load2sram(flash_addr + 256 * 2, *p_boot_flag, *p_app_location, (uint8_t *)UDFDEF_ADDR_WIN_RANGE, UDFDEF_ADCSAMP_NUM * 2, (eQSPI_FLASH_MODE)FLASH_INTERFACE);
        #elif UDFDEF_ADCSAMP_NUM == 256
        udsf_memory_load2sram(flash_addr, *p_boot_flag, *p_app_location, (uint8_t *)UDFDEF_ADDR_WIN_RANGE, UDFDEF_ADCSAMP_NUM * 2, (eQSPI_FLASH_MODE)FLASH_INTERFACE);
        #else
#error xxxxx
        #endif

        #endif

        #endif
    }
}

static int udsf_demo_radar_app_lp_trigger_dacq(void)
{

    #if( TEST_MODE == 300 )
    udsf_gpio_output_ctrl(1 << GPIO_OUTPUT_ID, 1);
    GPIO_OUTPUT_SET = 1;
    #endif

    //  #if (TEST_MODE != 0) && defined( GPIO_OUTPUT_ENABLE )
    //  GPIO_OUTPUT_SET = 1;
    //  #endif

    #if TEST_MODE == 100

    while (1)
    {

        uint32_t coef_a = udsf_dfc_fp_2_psf(0.85 * 8192, 0);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)&coef_a, 1, "coef_a(psf):", "\n");

        uint32_t coef_b = udsf_dfc_fp_2_psf(0.15 * 8192, 0);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)&coef_b, 1, "coef_b(psf):", "\n");

        uint32_t src0 = udsf_dfc_fp_2_psf(1 * 8192, 0);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)&src0, 1, "src0(psf):", "\n");

        uint32_t src1 = udsf_dfc_fp_2_psf(1 * 8192, 0);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)&src1, 1, "src1(psf):", "\n");

        *(uint32_t *)BBE_ABUF0_BASE = src0;
        *(uint32_t *)BBE_ABUF1_BASE = coef_a;
        udsf_bbe_p2_row_mul_coef(1, BBE_ABUF0_BASE, BBE_ABUF1_BASE, BBE_ABUF2_BASE);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)BBE_ABUF2_BASE, 1, "src0 mul coef a(psf):", "\n");

        *(uint32_t *)BBE_ABUF0_BASE = src1;
        *(uint32_t *)BBE_ABUF1_BASE = coef_b;
        udsf_bbe_p2_row_mul_coef(1, BBE_ABUF0_BASE, BBE_ABUF1_BASE, BBE_ABUF2_BASE + 4);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)(BBE_ABUF2_BASE + 4), 1, "src1 mul coef b(psf):", "\n");

        // 使用abs acc来实现abs add，参数2要大于参数1
        udsf_bbe_p2_row_abs_add(1, BBE_ABUF2_BASE, BBE_ABUF2_BASE + 4, BBE_ABUF0_BASE);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)(BBE_ABUF0_BASE), 1, "abs add(abs):", "\n");

        // abs格式转回伪浮点
        uint32_t abs_bg = *(uint32_t *)(BBE_ABUF0_BASE);
        *(uint32_t *)(BBE_ABUF0_CPX_BASE) = (abs_bg >> 3) & 0x7ffFFff;       // U32,16 -> S28,13,舍弃最高2bit位
        *(uint32_t *)(BBE_ABUF0_CPX_BASE + 4) = 0;

        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)(BBE_ABUF0_BASE), 1, "abs to psf(psf):", "\n");

        uint32_t abs_bg_abs = *(uint32_t *)(BBE_ABS_OFFSET + BBE_ABUF0_BASE);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)(&abs_bg_abs), 1, "abs to psf(abs):", "\n");


        uint32_t xxxx1 = udsf_dfc_fp_2_psf(1 * 8192, 0);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)&xxxx1, 1, "1(psf):", "\n");

        uint32_t xxxx2 = udsf_dfc_fp_2_psf(10 * 8192, 0);
        LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG, 1, (uint32_t *)&xxxx2, 1, "10(psf):", "\n");

        udsf_delay_ms(500);
        WDT->STR = 1;
        continue;
    }

    #endif

    //udsf_rtc_set(0, paramRT->frame_intv_ms * 34 - 100);

    udsf_data_acquisiton_chirpsum(1, 0, 0, NULL);

    return 0;
}
#if (TEST_MODE == 500)
static int udsf_demo_radar_app_lp_trigger_procA(void)
{

    /*调试数据输出*/
    #if defined(DEBUG_DATA_ENABLE) && (TEST_MODE>0)
    struct proto_data_cfg protoData_debug_cfg;
    {
        protoData_debug_cfg.Proto_MVersion = DEBUG_DATA_PROTOCOL_VER;
        int temp = paramRT->frame_intv_ms;

        if (temp > 127 * 10)
        {
            protoData_debug_cfg.frame_info.interval_10_100_ms = temp / 100 + 0x80;
        }

        else
        {
            protoData_debug_cfg.frame_info.interval_10_100_ms = temp / 10;
        }

        #if DEBUG_DATA_INTERFACE == 1
        #if DEBUG_DATA_PROTOCOL_VER > 1
        protoData_debug_cfg.send_data  = udsf_spi_send_data_trans;
        protoData_debug_cfg.send_frame = udsf_spi_send_data;
        #elif DEBUG_DATA_PROTOCOL_VER == 1
        protoData_debug_cfg.send_data  = udsf_spi_send_data;
        protoData_debug_cfg.send_frame = udsf_spi_send_data;
        #else
        protoData_debug_cfg.send_data  = udsf_spi_send_data;
        #endif
        #else
        #if DEBUG_DATA_PROTOCOL_VER > 1
        protoData_debug_cfg.send_data  = udsf_uart_send_data_trans;
        protoData_debug_cfg.send_frame = udsf_uart_send_data;
        #elif DEBUG_DATA_PROTOCOL_VER == 1
        protoData_debug_cfg.send_data  = udsf_uart_send_data;
        protoData_debug_cfg.send_frame = udsf_uart_send_data;
        #else
        protoData_debug_cfg.send_data  = udsf_uart_send_data;
        #endif
        #endif
    }
    #endif

    #if defined(DEBUG_DATA_ENABLE)
    protoData_debug_cfg.frame_info.draw_id = 1;
    #endif

    #if (DEBUG_DATA_INTERFACE>0)
    udsf_ft232_init();
    #endif

    #if 0   /* 输出时域chirps(原始ADC) */

    if (TEST_MODE_FRAME_FILTER(paramRT->frame_cnt, TEST_OUTPUT_N_FRAME))
    {
        protoData_debug_cfg.frame_info.frame_class  = eFRAME_TIMING_8BIT;
        protoData_debug_cfg.frame_info.samp_num     = UDFDEF_ADCSAMP_NUM;
        protoData_debug_cfg.frame_info.samp_ant     = 2/*UDFDEF_ANT_NUM*/; // 本demo强制采集2条天线
        protoData_debug_cfg.frame_info.samp_chirp   = UDFDEF_CHIRP_SUM_NUM;
        protoData_debug_cfg.frame_info.draw_name    = eDRAW_SMALL_FRAME;
        protoData_debug_cfg.frame_info.draw_opt     = 0;
        protoData_debug_cfg.pDataSrc                = (uint8_t *)UDFDEF_ADDR_ADCSAMP;

        protoData_debug_cfg.frame_info.draw_name_id = eDRAW_ANTALL;
        udsf_protocol_debug_send(0, &protoData_debug_cfg, -1, 1, 1, 0);
    }

    #endif


    P2_FFT_HANDLE(&paramALG->alg0);

    if (TEST_MODE_FRAME_FILTER(paramRT->frame_cnt, TEST_OUTPUT_N_FRAME))
    {
        protoData_debug_cfg.frame_info.frame_class  = eFRAME_TIMING_PSEUDO_FLOATING;
        protoData_debug_cfg.frame_info.samp_num     = UDFDEF_ADCSAMP_NUM;
        protoData_debug_cfg.frame_info.samp_ant     = UDFDEF_ANT_NUM;
        protoData_debug_cfg.frame_info.samp_chirp   = 1;
        protoData_debug_cfg.frame_info.draw_name    = eDRAW_CHIRP_SUM;
        protoData_debug_cfg.frame_info.draw_opt     = UDFDEF_CHIRP_SUM_NUM;
        protoData_debug_cfg.pDataSrc                = (uint8_t *)(UDFDEF_ADDR_CHIRPSUM + sUDF_global.frame_fifo_index * UDFDEF_SIZE_CHIRPSUM_PER_CHIRP);

        protoData_debug_cfg.frame_info.draw_name_id = eDRAW_ANTALL;
        udsf_protocol_debug_send(0, &protoData_debug_cfg, -1, 1, 1, 0);
    }

    return 0;
}

#else
static int udsf_demo_radar_app_lp_trigger_procA(void)
{


    /*调试数据输出*/
    #if defined(DEBUG_DATA_ENABLE) && (TEST_MODE>0)
    struct proto_data_cfg protoData_debug_cfg;
    {
        protoData_debug_cfg.Proto_MVersion = DEBUG_DATA_PROTOCOL_VER;
        int temp = paramRT->frame_intv_ms;

        if (temp > 127 * 10)
        {
            protoData_debug_cfg.frame_info.interval_10_100_ms = temp / 100 + 0x80;
        }

        else
        {
            protoData_debug_cfg.frame_info.interval_10_100_ms = temp / 10;
        }

        #if DEBUG_DATA_INTERFACE == 1
        #if DEBUG_DATA_PROTOCOL_VER > 1
        protoData_debug_cfg.send_data  = udsf_spi_send_data_trans;
        protoData_debug_cfg.send_frame = udsf_spi_send_data;
        #elif DEBUG_DATA_PROTOCOL_VER == 1
        protoData_debug_cfg.send_data  = udsf_spi_send_data;
        protoData_debug_cfg.send_frame = udsf_spi_send_data;
        #else
        protoData_debug_cfg.send_data  = udsf_spi_send_data;
        #endif
        #else
        #if DEBUG_DATA_PROTOCOL_VER > 1
        protoData_debug_cfg.send_data  = udsf_uart_send_data_trans;
        protoData_debug_cfg.send_frame = udsf_uart_send_data;
        #elif DEBUG_DATA_PROTOCOL_VER == 1
        protoData_debug_cfg.send_data  = udsf_uart_send_data;
        protoData_debug_cfg.send_frame = udsf_uart_send_data;
        #else
        protoData_debug_cfg.send_data  = udsf_uart_send_data;
        #endif
        #endif
    }
    #endif

    #if defined(DEBUG_DATA_ENABLE)
    protoData_debug_cfg.frame_info.draw_id = 1;
    #endif


    #if 1 && ((TEST_MODE == 1) /*|| (TEST_MODE == 2)|| (TEST_MODE == 3 )*/ ) /* 输出时域chirps(原始ADC) */

    if (TEST_MODE_FRAME_FILTER(paramRT->frame_cnt, TEST_OUTPUT_N_FRAME))
    {
        protoData_debug_cfg.frame_info.frame_class  = eFRAME_TIMING_8BIT;
        protoData_debug_cfg.frame_info.samp_num     = UDFDEF_ADCSAMP_NUM;
        protoData_debug_cfg.frame_info.samp_ant     = 2/*UDFDEF_ANT_NUM*/; // 本demo强制采集2条天线
        protoData_debug_cfg.frame_info.samp_chirp   = UDFDEF_CHIRP_SUM_NUM;
        protoData_debug_cfg.frame_info.draw_name    = eDRAW_SMALL_FRAME;
        protoData_debug_cfg.frame_info.draw_opt     = 0;
        protoData_debug_cfg.pDataSrc                = (uint8_t *)UDFDEF_ADDR_ADCSAMP;

        protoData_debug_cfg.frame_info.draw_name_id = eDRAW_ANTALL;
        udsf_protocol_debug_send(0, &protoData_debug_cfg, -1, 1, 1, 0);
    }

    #endif




    // adc->adc_chirpsum fifo
    P2_FFT_STEPCFG8_HANDLE(&paramALG->alg0, paramALG->alg0.dstInc * sUDF_global.frame_fifo_index);

    /* 输出时域CHIRP(chirpsum) */
    //  LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG,1,&fifo_start,1,"fifo start: ","\n");
    //  LOG_SEND_U32_BASE16(LOG_LEVEL_EMERG,1,&sUDF_global.frame_fifo_index,1,"fifo end: ","\n");
    //  udsf_uart_send_string("3333\n");

    P2_FFT_HANDLE(&paramALG->alg1);   //fft1d

    #if 1 && (TEST_MODE == 3 )
    uint8_t frame_head[] = {1, 2, 3, 4, 5, 6, 7, 8};
    protoData_debug_cfg.send_frame(frame_head, sizeof(frame_head));
    protoData_debug_cfg.send_data((uint8_t *)BBE_ABUF1_BASE, UDFDEF_RANGE_USE * 4);
    protoData_debug_cfg.send_data((uint8_t *)BBE_ABUF1_BASE + 8 * 1024, UDFDEF_RANGE_USE * 4);
    #endif


    P2_FFT_HANDLE(&paramALG->alg3);   //fft1d minus
    //  udsf_uart_send_string("4444\n");
    #if 1 && (TEST_MODE == 4 )
    uint8_t frame_head[] = {1, 2, 3, 4, 5, 6, 7, 8};
    protoData_debug_cfg.send_frame(frame_head, sizeof(frame_head));
    protoData_debug_cfg.send_data((uint8_t *)UDFDEF_ADDR_FFT1D_ANT1, UDFDEF_RANGE_USE * 4);
    protoData_debug_cfg.send_data((uint8_t *)UDFDEF_ADDR_FFT1D_MINUS, UDFDEF_RANGE_USE * 4);

    //    protoData_debug_cfg.send_data((uint8_t*)BBE_ABUF0_BASE+8*1024,UDFDEF_RANGE_USE*4);
    #endif

    udsf_memcpy32((uint32_t *)(UDFDEF_ADDR_FFT1D_ANT1), (uint32_t *)UDFDEF_ADDR_FRAME_RECOVER_FFT1D_OLD, UDFDEF_RANGE_USE * 4 * 2);

    uint32_t Pow_once, Pow_twice, find_one, find_two;
    Pow_once = 0;
    Pow_twice = 0;
    uint8_t indexnum_A1, indexnum_B1, index;
    indexnum_A1 = 0;
    indexnum_B1 = 0;
		index = 0;
    uint16_t frame_range;
    uint16_t index_range;
    uint32_t find_max;
		
    for (uint8_t i = 0; i < 1 ; i++)
    {
        for (uint8_t j = 1; j < 19 ; j++)  //23cm一格，3米是12格
        {
            find_max = Read_M32(BBE_ABUF2_BASE + UDFDEF_RANGE_USE * 4 + BBE_ABS_OFFSET + j * 4 + i * 32 * 4);
            find_one = Read_M32(BBE_ABUF2_BASE + UDFDEF_RANGE_USE * 4 + BBE_ABS_OFFSET + j * 4 - 4 + i * 32 * 4);
            find_two = Read_M32(BBE_ABUF2_BASE + UDFDEF_RANGE_USE * 4 + BBE_ABS_OFFSET + j * 4 + 4 + i * 32 * 4);

            //距离梯度确定能量阈值
            if(	find_max > find_one && find_max > find_two
							&& find_max > ( (500 + sUDF_global.i2c_reg5c * 100 + (sUDF_global.i2c_reg67 - j) * sUDF_global.i2c_reg68) > 500 ?
															(500 + sUDF_global.i2c_reg5c * 100 + (sUDF_global.i2c_reg67 - j) * sUDF_global.i2c_reg68) : 500) )
            {
                if (find_max > Pow_once)
                {
                    Pow_twice = Pow_once;
                    Pow_once = find_max;
                    indexnum_A1 = j;

                }
                else if (find_max > Pow_twice)
                {
                    Pow_twice = find_max;
                    indexnum_B1 = j;
                }
            }
        }
    }

    if (indexnum_B1 < indexnum_A1 && Pow_twice > (6 * Pow_once / 10) && indexnum_B1 != 0)
    {
//        index_range = indexnum_B1 * 23;
//        find_max = Pow_twice;
			index=indexnum_B1;
    }

    else
    {
//        index_range = indexnum_A1 * 23;
//        find_max = Pow_once;
			index=indexnum_A1;
    }

  uint32_t noflagtime = sUDF_global.i2c_reg42 + sUDF_global.i2c_reg43 * 256;
            if(index <= sUDF_global.i2c_reg12 && index != 0)
            {
                paramCFG->last_index_cnt++;
                paramCFG->one_noflag = 0;
            }
            else
            {
                if(paramCFG->one_noflag < 200)
                {
                    paramCFG->one_noflag++;
                }
            }

            if(paramCFG->last_index_cnt == 1 && index != 0)
            {
                paramCFG->min_index_range = index;
            }
            else if(index < paramCFG->min_index_range && index != 0)
            {
                paramCFG->min_index_range = index;
            }

            if(paramCFG->one_noflag > (noflagtime * 100 / paramCFG->frame_interval))
            {
                paramCFG->last_index_cnt = 0;
                GPIO0_5 = 0;
                GPIO0_2 = 0;
            }

            if(paramCFG->last_index_cnt > sUDF_global.i2c_reg61)
            {
                paramCFG->last_index_cnt = 0;

                if(paramCFG->min_index_range <= sUDF_global.i2c_reg10)
                {
                    GPIO0_5 = 1;
                    GPIO0_2 = 1;
                }else if(paramCFG->min_index_range <= sUDF_global.i2c_reg11)
                {
                    GPIO0_5 = 1;
                    GPIO0_2 = 0;
                }else if(paramCFG->min_index_range <= sUDF_global.i2c_reg12)
                {
                    GPIO0_5 = 0;
                    GPIO0_2 = 1;		
                }
								#if (UART_BAUDRATE>0)
								printf("sUDF_global.i2c_reg10 = %d\r\n",sUDF_global.i2c_reg10);
								printf("sUDF_global.i2c_reg11 = %d\r\n",sUDF_global.i2c_reg11);
								printf("sUDF_global.i2c_reg12 = %d\r\n",sUDF_global.i2c_reg12);
								printf("Range %dcm\r\n",index*23);
								udsf_delay_ms(1);
								#endif
            }

    paramCFG->no_target_cnt++;

			if (index <= (sUDF_global.i2c_reg12 > 17 ? sUDF_global.i2c_reg12 + 2 : 18) && index != 0)
    {
        paramCFG->frame_interval = sUDF_global.i2c_reg3e + sUDF_global.i2c_reg3f * 256;
        paramCFG->no_target_cnt = 0;
    }
    else
    {
        if (paramCFG->no_target_cnt > sUDF_global.i2c_reg4e + sUDF_global.i2c_reg4f * 256)
        {
            // 切换到低帧率
            paramCFG->frame_interval = sUDF_global.i2c_reg31 + sUDF_global.i2c_reg32 * 256;
            GPIO0_5 = 0;
            GPIO0_2 = 0;
        }
    }

    return 0;
}

#endif

#if defined(APP_HAS_MULTI_UDF) && (ENABLE_UDF13<1) && (ENABLE_UDF06<1)
    #error "udf13 or udf06 must enabled"
#endif

static int udsf_demo_radar_app_lp_trigger_procB(void)
{

    #if( TEST_MODE == 400 )
    GPIO_OUTPUT_SET = 1;
    #endif

    #if defined(APP_HAS_MULTI_UDF)
    #if ENABLE_UDF06>0
    LOG_SEND_STRING(LOG_LEVEL_DEBUG, "udf06 run\n");
    #else
    LOG_SEND_STRING(LOG_LEVEL_DEBUG, "udf13 run\n");
    #endif
    #endif

    /*调试数据输出*/
    #if 0 && defined(DEBUG_DATA_ENABLE) && (TEST_MODE>0)
    struct proto_data_cfg protoData_debug_cfg;
    {
        protoData_debug_cfg.Proto_MVersion = DEBUG_DATA_PROTOCOL_VER;
        int temp = paramRT->frame_intv_ms;

        if (temp > 127 * 10)
        {
            protoData_debug_cfg.frame_info.interval_10_100_ms = temp / 100 + 0x80;
        }

        else
        {
            protoData_debug_cfg.frame_info.interval_10_100_ms = temp / 10;
        }

        #if DEBUG_DATA_INTERFACE == 1
        #if DEBUG_DATA_PROTOCOL_VER > 1
        protoData_debug_cfg.send_data  = udsf_spi_send_data_trans;
        protoData_debug_cfg.send_frame = udsf_spi_send_data;
        #elif DEBUG_DATA_PROTOCOL_VER == 1
        protoData_debug_cfg.send_data  = udsf_spi_send_data;
        protoData_debug_cfg.send_frame = udsf_spi_send_data;
        #else
        protoData_debug_cfg.send_data  = udsf_spi_send_data;
        #endif
        #else
        #if DEBUG_DATA_PROTOCOL_VER > 1
        protoData_debug_cfg.send_data  = udsf_uart_send_data_trans;
        protoData_debug_cfg.send_frame = udsf_uart_send_data;
        #elif DEBUG_DATA_PROTOCOL_VER == 1
        protoData_debug_cfg.send_data  = udsf_uart_send_data;
        protoData_debug_cfg.send_frame = udsf_uart_send_data;
        #else
        protoData_debug_cfg.send_data  = udsf_uart_send_data;
        #endif
        #endif
    }

    protoData_debug_cfg.frame_info.draw_id = sUDF_global.draw_id;

    #endif


    #if( TEST_MODE == 400 )
    GPIO_OUTPUT_SET = 0;
    #endif

    #if ENABLE_UDF13 > 0
    #if (SDK_VERSION < SDK_VERSION_GENE(3,0))
    int res = UNIVERSAL_UDF_RES_GO_UDF03_NO_NEED_LOAD; // 仅配合UDF13使用时可使用NO_NEED_LOAD返回值。 不重新加载UDF03加载直接调度至UDF03
    #else
    int res = UNIVERSAL_UDF_RES_GO_UDF05_NO_NEED_LOAD; // 仅配合UDF13使用时可使用NO_NEED_LOAD返回值。 调度至UDF05，下次不再重新加载UDF13
    #endif
    #if defined(APP_HAS_MULTI_UDF) && (SDK_VERSION < SDK_VERSION_GENE(3,0))
    udsf_app_task_info_recovery(&sUDF_global.task_info);
    #endif
    #else
    #if (SDK_VERSION < SDK_VERSION_GENE(3,0))
    int res = UNIVERSAL_UDF_RES_GO_UDF03;
    #else
    int res = UNIVERSAL_UDF_RES_GO_UDF05;
    #endif
    #endif
    struct pc_rst *frameRst = (struct pc_rst *)(UDFDEF_ADDR_POINT_RST);

    #if defined(RADAR_MOTION_DETECT)
    struct pc_rst *frameRst_radar1 = (struct pc_rst *)(UDFDEF_ADDR_POINT_RST_MOTION);
    #endif

    #if defined(RADAR_MOTION_DETECT)

    if ((frameRst->targetNum > 0) || (frameRst_radar1->targetNum > 0))
    #else
    if (frameRst->targetNum > 0)
    #endif
    {
        #if defined(GPIO_OUTPUT_ENABLE)
        udsf_gpio_output(GPIO_OUTPUT_ID, GPIO_OUTPUT_ACTIVE);    // 有目标
        #endif
        sUDF_global.has_target_cnt_lck = paramRT->frame_cnt;
    }

    else
    {
        if (paramRT->frame_cnt - sUDF_global.has_target_cnt_lck > paramCFG->no_target_cnt_timeout)
        {
            #if defined(GPIO_OUTPUT_ENABLE)
            udsf_gpio_output(GPIO_OUTPUT_ID, !GPIO_OUTPUT_ACTIVE);   // 无目标
            #endif
        }
    }

    #if( TEST_MODE == 400 )
    GPIO_OUTPUT_SET = 1;
    #endif
#define DEFINED_PC_RST pc_rst_t
#define DEFINED_EPC    epc_t
#define P_PC_RST       struct pc_rst
#define P_EPC          struct epc

    //  P_PC_RST * frameRst        = (P_PC_RST *)(UDFDEF_ADDR_POINT_RST);
    //  #if defined(RADAR_MOTION_DETECT)
    //  P_PC_RST * frameRst_radar1 = (P_PC_RST *)(UDFDEF_ADDR_POINT_RST_MOTION);
    //  #endif

    //udf03 后半段： 点云输出,[应用切换],帧间低功耗
    #if 1 && ((TEST_MODE == 0) || (TEST_MODE==203) || (TEST_MODE==400)) && defined(POINTRST_ENABLE)

    udsf_epc_init((DEFINED_EPC *)UDFDEF_ADDR_EPC_STRUCT,
                  UDFDEF_RANGE_USE, UDFDEF_CHIRP_NUM, RANGE_RES_MM, VEL_RES_MM_S, 0, 0, 0);
    udsf_pcrst_append_to_epc((P_PC_RST *)frameRst, (P_EPC *)UDFDEF_ADDR_EPC_STRUCT, 2, 2);       // 微动
    #if defined(RADAR_MOTION_DETECT)
    udsf_pcrst_append_to_epc((P_PC_RST *)frameRst_radar1, (P_EPC *)UDFDEF_ADDR_EPC_STRUCT, 0, 1);       // 运动
    #endif
    uint8_t *packRst = (uint8_t *)frameRst;
    int len = 0;
    uint16_t target_cnt = 0;
    /*protocv12 输出*/
    udsf_protov12_frame_start(packRst, &len,/*paramRT->frame_intv_ms*/FRAME_INTV_MS, (P_EPC *)UDFDEF_ADDR_EPC_STRUCT, 0x12, 0);
    udsf_protov12_add_epc_data(packRst, &len, (P_EPC *)UDFDEF_ADDR_EPC_STRUCT, &target_cnt);
    udsf_protov12_frame_end(packRst, &len, target_cnt);

    #if ( POINTRST_INTERFACE == 1 )
    FUNC_DATA_SEND_t point_cloud_output = udsf_spi_send_data;
    #else
    FUNC_DATA_SEND_t point_cloud_output = udsf_uart_send_data;
    #endif
    point_cloud_output(packRst, len);
    #endif



    CSTD_PRINTF("frame end\n");
    //  #if !defined(APP_INDEPENDENT)
    //  #error 暂未适配 "非独立应用 多udf切换"
    //  for( int i = 0;i< frameRst->targetNum;i++ ){
    //      if( frameRst->target[i].d1Idx < paramCFG->trigger_range_threshold ){
    //              uint32_t addr = udsf_udk_addr_get_app_default(p_rom_gb->app_location);
    //              udsf_app_switch(addr,p_rom_gb->boot_flag,p_rom_gb->app_location, // 切换回主应用
    //                                              paramRT->ab_app_flag,paramFunc->ab_area_offset);
    //              paramRT->switch_flag = 1; // return 前应该置位该标志位
    //              return UNIVERSAL_UDF_RES_RESET; // 重置调度任务
    //      }
    //  }
    //  #endif

    #if( TEST_MODE == 400 )
    GPIO_OUTPUT_SET = 0;
    #endif

    paramRT->switch_flag = 1; // return 前应该置位该标志位
    return res;
}

static int udsf_demo_radar_app_lp_trigger_main(void)
{

    if (paramRT->startup_flag)
    {
        udsf_demo_radar_app_lp_trigger_init();

//        #if defined(VCO_OUTPUT_ENABLE)
//        udsf_ramp_vctrl_enable();
//        #endif

//        #ifdef UART_RECV_ENABLE
//        udsf_uart_recv_init();
//        #endif
        #if (UART_BAUDRATE > 0)

        #else
        udsf_i2c_init();
        #endif

        paramRT->startup_flag = 0;
        udsf_i2c_reg_init();
        paramCFG->frame_interval = sUDF_global.i2c_reg31 + sUDF_global.i2c_reg32 * 256;

    }

    while (1)
    {
        udsf_i2c_salve_check_run();
        WDT->STR = 1;

        if (sUDF_global.i2c_regaa == 0xF1 || sUDF_global.i2c_regaa == 0xF7)
        {
            paramRT->switch_flag = 1; // return 前应该置位该标志位
            return UNIVERSAL_UDF_RES_GO_UDF11;
        }

        if (paramCFG->ota_status == OTA_STATUS_IDLE)
        {
            GPIO->INTPOL = (2 << (6 << 1));
            sUDF_global.state = PRE_TRIGGER_TIME;
        }


        if (paramRT->switch_flag == 0)
        {
					  udsf_demo_radar_app_lp_trigger_dacq();
									
            udsf_demo_radar_app_lp_trigger_procA();
        }

        udsf_rtc_set(0, (paramCFG->frame_interval) * 34 - 100);
        /*sleep enter -> lrc/128 clk*/
        udsf_lpw_enter();

        /*fake sleep*/
        udsf_iic_wakeup(0);
        //      udsf_rtc_get_block(0,udsf_wakeup_check); // wait rtc0

        /*sleep exit -> lrc/1 clock*/
        udsf_lpw_exit();

        /*full-speed run*/
        udsf_full_speed();

        paramRT->frame_cnt++;
        paramRT->switch_flag = 0;
    }
}

#endif

#endif
static int udsf_demo_radar_app_lp_trigger_udf06()
{
    udsf_demo_radar_app_lp_trigger_dacq();
    udsf_demo_radar_app_lp_trigger_procA();
    paramRT->switch_flag = 1; // return 前应该置位该标志位
    return UNIVERSAL_UDF_RES_GO_UDF05;
}

static int udsf_demo_radar_app_lp_trigger_param_cfg()
{

        GPIO0_2 = 0;
        GPIO0_5 = 0;
        sUDF_global.state = RESPOND;

        //udsf_i2c_reg_init();
//    while(1)
//    {
//        WDT->STR = 1;
//        GPIO->INTPOL = (2 << (6 << 1));
//        udsf_i2c_salve_check_run_multidata();
//    }

        // close wdt
        NVIC->ICER[0] = (1 << ((uint32_t)(WDT_IRQn) & 0x1F));
        SYSC->CLK_EN &= ~WDT_PATTERN_EN_Msk;
        SYSC->CLK_EN |= (WDT_PATTERN_EN_Msk & (WDT_PATTERN_DISABLE << WDT_PATTERN_EN_Pos)); // Close WDT clock

#define PAGE_PACK_LEN (128)

        int res = UNIVERSAL_UDF_RES_GO_UDF05;
        int page_cnt = 1;
        uint32_t download_addr = BBE_ABUF0_BASE;
        paramCFG->ota_status = OTA_STATUS_DOWNLOAD;

        while(1)
        {
            udsf_i2c_salve_check_run_multidata();

            //参数修改完成退出
            if(sUDF_global.i2c_reg00 == 1)
            {
                sUDF_global.i2c_regaa = 0xF0;
                sUDF_global.i2c_reg00 == 0x00;
                sUDF_global.state = 0;
                sUDF_global.binpage_cnt = 0;
                paramCFG->ota_status = OTA_STATUS_IDLE;
                paramRT->switch_flag = 1;
                return UNIVERSAL_UDF_RES_GO_UDF05;
                break;
            }

            if( sUDF_global.i2c_regaa == 0xF4)
            {
                udsf_wdt_rstnow();

                while(1);
            }

            if( paramCFG->ota_status == OTA_STATUS_DOWNLOAD && sUDF_global.i2c_regaa == 0xF3)
            {
                paramCFG->ota_status = OTA_STATUS_OTA_WRITE;
            }

            if( paramCFG->ota_status == OTA_STATUS_DOWNLOAD && sUDF_global.i2c_regaa != 0xF7)
            {

//            #if OTA_SIZE_MAX > BBE_SRAM_SIZE // 本demo 仅演示 小于40KB OTA应用。
//			#error OTA_SIZE_MAX > BBE_SRAM_SIZE
//            #endif

                /* 仅演示验证使用，真实OTA下载流程客户自行实现 */
                #if defined(FAKE_RECV) // fake download. copy current area -> sram(BBE 40K)
                udsf_uart_send_string("[OTA Demo]: FAKE RECV...\n");

                // 读当前程序到 BBE_SRAM
                udsf_memory_load2sram(USRBOOT_UDP_DEFAULT_OFFSET + (paramFunc->ab_area_flag - 1)*OTA_SIZE_MAX, p_gb_sysboot->boot_flag, p_gb_sysboot->udt_src, \
                                      (uint8_t*)download_addr, OTA_SIZE_MAX);

                paramCFG->ota_status = OTA_STATUS_OTA_WRITE;
                sUDF_global.ota_len = OTA_SIZE_MAX;
                #else
                // demo uart-ota

                //uint8_t* pData8 = (uint8_t *)(sUDF_global.recv_buff);

                if(page_cnt == sUDF_global.binpage_cnt)
                {
//                for(int sCnt = 0; sCnt < PAGE_PACK_LEN; sCnt++)
//                {
//                    pData8[sCnt] = sUDF_global.i2c_regbb[sCnt];
//                }

                    sUDF_global.i2c_regaa = sUDF_global.binpage_cnt;

                    if(    (sUDF_global.i2c_regbb[0] == 'A') && (sUDF_global.i2c_regbb[1] == 'T')
                            && (sUDF_global.i2c_regbb[2] == '+') && (sUDF_global.i2c_regbb[3] == 'C')
                            && (sUDF_global.i2c_regbb[4] == 'R') && (sUDF_global.i2c_regbb[5] == 'C') )
                    {

                        uint16_t crc = 1;

                        for(int sCnt = 17; sCnt < PAGE_PACK_LEN; sCnt++)
                        {
                            if( sUDF_global.i2c_regbb[sCnt] != 0xA5 )
                            {
                                crc = 0;
                            }
                        }

                        if( crc == 1 )
                        {

                            uint32_t len = (sUDF_global.i2c_regbb[3 + 11] << 24) | (sUDF_global.i2c_regbb[2 + 11] << 16)
                                           | (sUDF_global.i2c_regbb[1 + 11] << 8) | sUDF_global.i2c_regbb[0 + 11];

                            if( len <= BBE_SRAM_SIZE )
                            {
                                crc = sUDF_global.i2c_regbb[1 + 15] << 8 | sUDF_global.i2c_regbb[0 + 15];

                                uint16_t Crc16 = udsf_get_crc((uint8_t*)download_addr, len);

                                if( Crc16 == crc )
                                {
//									udsf_uart_send_string("AT+CRC=OK\n");
                                    paramCFG->ota_status = OTA_STATUS_OTA_WRITE;
                                    paramCFG->ota_len = len;

                                    if(p_rom_gb->cfg_place == APP_PLACE_A_AREA)
                                    {
                                        sUDF_global.i2c_regaa = 0xFB;
                                    }
                                    else if(p_rom_gb->cfg_place == APP_PLACE_B_AREA)
                                    {
                                        sUDF_global.i2c_regaa = 0xFA;
                                    }

                                    page_cnt = 0;
//                                GPIO0_2 = 1;
//                                GPIO0_5 = 1;
                                    //udsf_delay_ms(20);
                                    //break;
//                                GPIO0_2 = 1;
                                    //while(1);
                                }
                                else
                                {
                                    //paramCFG->ota_status = OTA_STATUS_OTA_WRITE;
                                    sUDF_global.i2c_regaa = 0xFC;
                                }
                            }
                            else
                            {
                                sUDF_global.i2c_regaa = 0xFC;
//								udsf_uart_send_string("AT+CRC=ERR\n");
                                //while(1);
                            }
                        }
                    }
                    else
                    {

                        udsf_memcpy(sUDF_global.i2c_regbb, (uint8_t*)download_addr + PAGE_PACK_LEN * (sUDF_global.binpage_cnt - 1), PAGE_PACK_LEN);
                        page_cnt++;
//					udsf_uart_send_string("AT+OK\n");
                    }
                }

                #endif
            }

            if( paramCFG->ota_status == OTA_STATUS_OTA_WRITE && sUDF_global.i2c_regaa == 0xF2)
            {
//				udsf_uart_send_string("[OTA Demo]: WRITE OTA...\n");

//            // 写新ota固件到 新程序分区
//            uint32_t start_addr = USRBOOT_UDP_DEFAULT_OFFSET + (2 - paramFunc->ab_area_flag) * OTA_SIZE_MAX;
//            uint32_t len = sUDF_global.ota_len;
//            udsf_memory_program(start_addr, \
//                                p_gb_sysboot->boot_flag, p_gb_sysboot->udt_src, (uint8_t*)(download_addr), len);

//            sUDF_global.ota_status = OTA_STATUS_INFO_WRITE;

//            sUDF_global.state = 0;

                QSPI->CTRL = 7  << QSPI_CTRL_CLK_DIV_Pos     |
                             QSPI_STANDARD  << QSPI_CTRL_FRAME_MODE_Pos;

                // 更新SN
                ((UDP_2K_t*)download_addr)->paraRT.sn = paramRT->sn + 1;
                ((UDP_2K_t*)download_addr)->paraRT.crcVal = udsf_get_crc((uint8_t*) & (((UDP_2K_t*)download_addr)->paraRT), sizeof(PARAM_RUNTIME) - 2);

                // 写新ota固件到 新程序分区
                uint32_t start_addr = UDK_LITE_UDP_START_ADDR + ((paramRT->ab_app_flag - APP_PLACE_A_AREA + 1) % 2) * OTA_SIZE_MAX;
                uint32_t len = paramCFG->ota_len;
                udsf_memory_erase_and_program(start_addr, \
                                              p_rom_gb->boot_flag, p_rom_gb->app_location, (uint8_t*)(download_addr), len, eQSPI_FLASH_STD);

                paramCFG->ota_status = OTA_STATUS_INFO_WRITE;

            }
            else if( paramCFG->ota_status == OTA_STATUS_OTA_WRITE && sUDF_global.i2c_regaa == 0xF3)
            {
                sUDF_global.i2c_regaa = 0xF1;
                paramCFG->ota_status = OTA_STATUS_DOWNLOAD;
                page_cnt = 1;
                sUDF_global.binpage_cnt = 0;
            }

            if( paramCFG->ota_status == OTA_STATUS_INFO_WRITE )
            {
////				udsf_uart_send_string("[OTA Demo]: WRITE INFO...\n");
//            uint32_t sn;
//            uint32_t sn_bu;
//            // 读当前运行分区 sn@INFO
//            udsf_memory_load2sram(USRBOOT_INFO_OFFSET + (paramFunc->ab_area_flag - 1)*USRBOOT_INFO_SIZE_PRE + UDP_SIZE, p_gb_sysboot->boot_flag, p_gb_sysboot->udt_src, \
//                                  (uint8_t*)&sn, 4);
//            udsf_memory_load2sram(USRBOOT_INFO_OFFSET + (paramFunc->ab_area_flag - 1)*USRBOOT_INFO_SIZE_PRE + UDP_SIZE + 4, p_gb_sysboot->boot_flag, p_gb_sysboot->udt_src, \
//                                  (uint8_t*)&sn_bu, 4);

//            if( sn + sn_bu != 0xffffffff )
//            {
//                sn = 0; // 当前分区对应INFO区为空（首次OTA 两个INFO区均为空）
//            }

//            // sn+1
//            sn += 1;
//            *(uint32_t*)(download_addr + UDP_SIZE)   = sn;
//            *(uint32_t*)(download_addr + UDP_SIZE + 4) = 0xffffffff - sn;

//            // 写新分区 INFO
//            udsf_memory_program(USRBOOT_INFO_OFFSET + (2 - paramFunc->ab_area_flag)*USRBOOT_INFO_SIZE_PRE, \
//                                p_gb_sysboot->boot_flag, p_gb_sysboot->udt_src, (uint8_t*)(download_addr), FLASH_SECTOR_SIZE);

//            //udsf_uart_send_string("[OTA Demo]: WILL RESET...\n");
//            //udsf_udf_switch(3, 1, 0);
// 写新INFO
                udsf_memory_erase_and_program(SYSBOOT_AB_INFO_OUTER + ((paramRT->ab_app_flag - APP_PLACE_A_AREA + 1) % 2)*SYSBOOT_AB_INFO_PER_SIZE, \
                                              p_rom_gb->boot_flag, p_rom_gb->app_location, (uint8_t*)(download_addr), FLASH_SECTOR_SIZE, eQSPI_FLASH_STD);
                udsf_wdt_rstnow();

                while(1);
            }

        }

        paramRT->switch_flag = 1;
        return res;

}
